/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.app.controller;


import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.app.form.LoginForm;
import io.renren.modules.app.req.SmsLoginReq;
import io.renren.modules.app.req.weixin.WeChatGetUserInfoReq;
import io.renren.modules.app.req.weixin.WeChatLoginReq;
import io.renren.modules.app.service.CustomerService;
import io.renren.modules.app.service.WeChatLoginService;
import io.renren.modules.app.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * APP登录授权
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/api/login")
@Api(tags = "APP登录接口")
@Slf4j
public class AppLoginController {
    @Autowired
    private CustomerService customerService;
    @Autowired
    private JwtUtils jwtUtils;
    @Resource
    private WeChatLoginService weChatLoginService;

    /**
     * renren登录
     */
    @PostMapping("/testLogin")
    @ApiOperation("renren登录")
    public R testLogin(@RequestBody LoginForm form){
        //表单校验
        ValidatorUtils.validateEntity(form);

        //用户登录
        long userId = customerService.testLogin(form);

        //生成token
        String token = jwtUtils.generateToken(userId);

        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("expire", jwtUtils.getExpire());

        return R.ok(map);
    }

    /**
     * 账号密码登录
     */
    @PostMapping("login")
    @ApiOperation("账号密码登录")
    public R login(@RequestBody LoginForm form){
        //表单校验
        ValidatorUtils.validateEntity(form);
        //用户登录
        Map map = customerService.login(form);
        return R.ok(map);
    }
    /**
     * 短信登录
     */
    @PostMapping("smsLogin")
    @ApiOperation("短信登录")
    public R smsLogin(@RequestBody SmsLoginReq smsLoginReq){
        //表单校验
        ValidatorUtils.validateEntity(smsLoginReq);

        //用户登录
        Map map = customerService.smsLogin(smsLoginReq);
        return R.ok(map);
    }
    /**
     * @Description: 微信授权登录
     * @Param:
     * @return:
     * @Author:
     * @Date:
     */
    @PostMapping("/wxLogin")
    @ApiOperation("微信授权登录")
    public R wxLogin(@RequestBody WeChatLoginReq weChatLoginReq) {
        //用户登录
        Map map = customerService.wxLogin(weChatLoginReq);
        return R.ok();
    }
//    /**
//     * @Description: 微信授权登录
//     * @Param:
//     * @return:
//     * @Author:
//     * @Date:
//     */
//    @GetMapping("/wxLogin")
//    @ApiOperation("微信授权登录")
//    public R wxLogin(WeChatGetUserInfoReq weChatGetUserInfoReq) {
//        log.info("====店家获取信息请求参数====controller");
//        weChatLoginService.getAndAddMerchantInfo(weChatGetUserInfoReq);
//        return R.ok();
//    }

}
